package net.diaowen.dwsurvey.controller;

import net.diaowen.common.base.entity.Group;
import net.diaowen.common.base.entity.User;
import net.diaowen.common.base.service.AccountManager;
import net.diaowen.common.plugs.page.Page;
import net.diaowen.common.utils.PushThread;
import net.diaowen.dwsurvey.service.GroupManager;
import net.diaowen.dwsurvey.service.SurveyGroupManager;
import net.diaowen.dwsurvey.service.UserManager;
import net.diaowen.dwsurvey.service.WechatManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/sy/group/group-admin")
public class GroupAdminController{
	
	protected final static String RESOURCE_PATH = GroupAdminController.class.getResource("/template").getPath();
	
	protected final static String TEMP_PATH = "WEB-INF/temp/";
	
	@Autowired
	private GroupManager groupManager;
	
	@Autowired
	private AccountManager accountManager;

	@Autowired
	private SurveyGroupManager surveyGroupManager;
	@Autowired
	private WechatManager wechatManager;
	@Autowired
	private UserManager userManager;

	@RequestMapping("/list.do")
	public ModelAndView list(HttpServletRequest request,Page<Group> page,String userGroupName) throws Exception {
		ModelAndView modelAndView = new ModelAndView();
		try{
			Group entity = new Group();
			entity.setUserGroupName(userGroupName);
			page=groupManager.findPage(page,entity);
			modelAndView.addObject("page",page);
			modelAndView.addObject("userGroupName",userGroupName);
		}catch (Exception e) {
			e.printStackTrace();
		}
		modelAndView.setViewName("/diaowen-usergroup/list");
		return modelAndView;
	}

	/**
	 * 问卷添加小组
	 * @param entity
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/save.do")
	public String save(Group entity) throws Exception {
		try{
			String id = entity.getId();
			User curUser = accountManager.getCurUser();
			if(id==null || id.equals("")){
				entity.setId(null);
				entity.setOrgId(curUser.getOrgId());
				groupManager.adminSave(entity);
			}else{
				Group group = groupManager.get(id);
				group.setUserGroupName(entity.getUserGroupName());
				group.setOrgId(curUser.getOrgId());
				groupManager.adminSave(group);
			}
		}catch (Exception e){
			e.printStackTrace();
		}
		return "redirect:/sy/group/group-admin/list.do";
	}

	@RequestMapping("/input.do")
	public ModelAndView input(String id) throws Exception {
		ModelAndView modelAndView = new ModelAndView();
		if(id!=null){
			Group group = groupManager.getModel(id);
			modelAndView.addObject("entity",group);
			modelAndView.addObject("id",id);
		}
		modelAndView.setViewName("/diaowen-usergroup/input");
		return modelAndView;
	}
	
	/**
	 * 小组删除
	 */
	@RequestMapping("/delete.do")
	@ResponseBody
	public String delete(String id,String event) throws Exception {
		int result = 0;
		try{
			if(event!=null && "delete".equals(event)){
				result = groupManager.deleteGroup(id);
			}else{
				//groupManager.disUser(id);
			}
		}catch (Exception e) {
			result = 2;
			e.printStackTrace();
		}
		return String.valueOf(result);
	}
	
	@RequestMapping("/import.do")
	public ModelAndView importGroupUser(String id) throws Exception {
		ModelAndView modelAndView = new ModelAndView();
		if(id!=null){
			modelAndView.addObject("id",id);
		}
		modelAndView.setViewName("/diaowen-usergroup/import");
		return modelAndView;
	}

	@RequestMapping("/user.do")
	public ModelAndView showGroupUser(String id) throws Exception {
		ModelAndView modelAndView = new ModelAndView();
		if(id!=null){
			modelAndView.addObject("groupId",id);
		}
		modelAndView.setViewName("/diaowen-usergroup-sub/list");
		return modelAndView;
	}
	
	@RequestMapping("/checkGroupName.do")
	@ResponseBody
	public String checkGroupName(String userGroupName,String userGroupNameOra) throws Exception{
		User user = accountManager.getCurUser();
		Group group = groupManager.findByGroupNameAndOrgId(userGroupName,user.getOrgId());
		String result="true";
		if(group!=null && userGroupNameOra != null && !group.getUserGroupName().equals(userGroupNameOra) ){
			result="false";
		}
		return result;
	}


	@RequestMapping("/pushMessage.do")
	@ResponseBody
    public String pushMessage(String groupId,String surveyName,HttpServletRequest request)  {
    // 获取当前用户判断其身份权限
		String result = "false";
		// System.out.println("groupId = " + groupId);
		// System.out.println("进入了-------surveyName = " + surveyName);
		User user = accountManager.getCurUser();

		if (user!=null)
        {
            String userId = user.getId();
            //判断用户角色是不是管理员角色
            if(user.getSystemRole()==9)
			{
				Group group = groupManager.findOne(groupId);
				// Group group = groupManager.(groupId);
				if (group!=null)
				{
					System.out.println("进入pushMessage------");
					List<Object[]> userList = surveyGroupManager.getGroupWinUser(groupId);
					// List<Object[]> userList = userManager.findUserListByGroupId(groupId);
					System.out.println("userList.size() = " + userList.size());
					for (int i = 0; i < userList.size(); i++) {
						String WxId = String.valueOf(userList.get(i));
						PushThread pushThread = new PushThread();
						pushThread.setToUser(WxId);
						pushThread.setSurveyGroupManager(surveyGroupManager);
						pushThread.setUserName(user.getName());
						pushThread.setServeyName(surveyName);
						pushThread.start();
					}
					request.getSession().setAttribute("getNotifications",surveyName);
				}
			}
			result = "true";
        }

		return result;
        // return "redirect:list.do";
    }


}
